package com.sskj.app.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fegin.bean.app.StockUser;
import com.sskj.app.mapper.StockUserMapper;
import com.sskj.app.service.StockUserService;
import com.sskj.app.util.cacheUtil.JedisUtil;
import com.util.Respons.ResponseUtil;
import com.util.auth.AuthSign;
import com.util.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 手机用户表 服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2019-02-19
 */
@Service
public class StockUserServiceImpl extends ServiceImpl<StockUserMapper, StockUser> implements StockUserService {
    @Autowired
    JedisUtil jedisUtil;

    @Override
    public Map login(String account, String pswd) {
        Wrapper<StockUser> wrapper = new QueryWrapper<>();
        ((QueryWrapper<StockUser>) wrapper).eq("tel", account)
                .eq("pswd", pswd)
                .or()
                .eq("email", account)
                .eq("pswd", pswd).
                or()
                .eq("user_uid", account)
                .eq("pswd", pswd);
        List<StockUser> list = baseMapper.selectList(wrapper);
        //存在该用户时候
        if (list.size() > 0) {
            String token = null;
            try {
                token = AuthSign.sign(list.get(0).getUsername(), list.get(0).getId() + "");
                jedisUtil.setList(token, list, AuthSign.EXPIRE_TIME / 1000);
            } catch (Exception e) {
                throw new CommonException("令牌签发失败");
            }
            return ResponseUtil.getSuccessMap(token, "登陆成功");
        } else {
            return ResponseUtil.loginFail();
        }
    }

    @Override
    public Map saveOrUpdateStockUser(StockUser stockUser) {
        if (stockUser.getId() == null) {
            if (stockUser.getEmail() != null) {
                Wrapper<StockUser> wrapper = new QueryWrapper<>();
                Map map = new HashMap();
                map.put("email", stockUser.getEmail());
                Collection list = listByMap(map);
                if (list.size() > 0) {
                    return ResponseUtil.getNotNormalMap("该邮箱已注册");
                }
            }
            if (stockUser.getTel() != null) {
                Wrapper<StockUser> wrapper = new QueryWrapper<>();
                Map map = new HashMap();
                map.put("tel", stockUser.getTel());
                Collection list = listByMap(map);
                if (list.size() > 0) {
                    return ResponseUtil.getNotNormalMap("该手机号已注册");
                }
            }
            if (save(stockUser)) {
                return ResponseUtil.getSuccessMap();
            }
        } else {
            if (updateById(stockUser)) {
                return ResponseUtil.getSuccessMap();
            }
        }
        return ResponseUtil.getExceptionMap();

    }
}
